package workforce.limousine.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import limousine.vo.Users;
import limousine.dbservice.ModuleService;
import limousine.util.Service;
import limousine.util.Token;

public class ModuleManagerDelete extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        System.out.println("***ModuleManagerDelete");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /*if (!user.getRole_name().equals("Admin")) {
            System.out.println("--Not Admin Hack--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        // delete
        String id = request.getParameter("did");
        if (id != null) {// delete before
            ModuleService ms = new ModuleService();
            ms.setDataSource(ds);
            ms.deleteById(id);
        }
        session.setAttribute(Token.MSG_USER,Token.MSG_DELETE_SUCCESS);

        return mapping.findForward(Token.SUCCESS);
    }
}
